/*
 * TextMarker.java
 *
 * Created on 6 maj 2007, 20:55
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package krakow.ae.knp.wikiboard.gui.tools.markers;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import javax.swing.JOptionPane;
import krakow.ae.knp.wikiboard.gui.Board;
import krakow.ae.knp.wikiboard.gui.tools.*;

/**
 *
 * @author m
 */
public class TextMarker extends FigureMarker {
    
    private final int PADDING = 2;
    private static int instance_counter = 1;
    private Point startPoint;
    private BufferedImage text;
    
    /** Creates a new instance of TextMarker */
    public TextMarker() {
        super( "Text" + instance_counter++ );
        text = null;
        startPoint = null;
    }

    public void drawFigure(Graphics2D g, boolean dragged) {
        g.setXORMode(Board.bgColor);
        if(!dragged)
            g.setPaintMode();
        g.setColor(Board.fgColor);
        g.drawImage(text, startPoint.x, startPoint.y, null);
    }

    public void mousePressed(int x, int y, Graphics2D g) {
        String string = JOptionPane.showInputDialog("Podaj tekst: ");
        startPoint = new Point(x, y);
        int height = g.getFontMetrics().getHeight();
        int width = g.getFontMetrics().stringWidth(string);
        
        text = new BufferedImage(width+PADDING, height+PADDING, BufferedImage.TYPE_INT_ARGB);
        Graphics2D textG = text.createGraphics();
        textG.setColor(Board.fgColor);
        textG.drawString(string, 0, height);
        textG.dispose();
        
        drawFigure(g, false);
    }

    public void mouseDragged(int x, int y, Graphics2D g) {
        startPoint.x = x;
        startPoint.y = y;
    }

    public boolean mouseReleased(int x, int y, Graphics2D g) {
        return true;
    }

    public boolean contains(Point2D p) {
        return ( (p.getX() >= startPoint.x) 
            &&     (p.getX() < (startPoint.x + text.getWidth()))
            &&     (p.getY() >= startPoint.y)
            &&     (p.getY() < (startPoint.y + text.getHeight())) );
    }

    public void move(double x, double y) {
        startPoint.x += (int) x;
        startPoint.y += (int) y;
    }
    
}
